package userinterface;

import java.awt.Label;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JOptionPane;

import alList.StuGPA;
import db.DBquery;

public class QueryByStuID extends QueryInfo {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public QueryByStuID() {
		super("QueryByStuID", "Please input the student ID:");
		
		preview.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				id = textField.getText();
				try {
					DBquery q = new DBquery();
					List<StuGPA> stuGPAList = q.qGPAByStuID(id);
					Preview.previewGPA(stuGPAList);
				} catch (Exception e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "Read failed!");
				}

			};
		});
		
		export.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				try {
					id = textField.getText();
					DBquery q = new DBquery();
					List<StuGPA> stuGPAList = q.qGPAByStuID(id);
					
					try {
						String urlString = exportFile.getText();
						export(urlString, stuGPAList);

						Label label = new Label("Successfully export into Excel result!");
						container.add(label);
						label.setBounds(200, 200, 300, 30);
						
					} catch (Exception e2) {
						e2.printStackTrace();
						JOptionPane.showMessageDialog(null, "Invalid Excel File URL!");
					}
					
				} catch (Exception e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "The Student ID read failed!");
				}
				
			};
		});
		
	}

}
